package com.forestar.xzk.commonview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.Scroller;

import androidx.annotation.Nullable;

import com.forestar.xzk.logs.LogTag;

/**
 * 移动canvas  ，会发生什么呢？
 *
 */
public class MoveCanvasView extends View implements GestureDetector.OnGestureListener {

    private GestureDetector gestureDetector;
    private Scroller scroller;

    private Paint paint;

    public MoveCanvasView(Context context) {
        this(context,null);
    }

    public MoveCanvasView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MoveCanvasView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
        init();
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

    }

    private void initData(){
    }

    private void init(){
        gestureDetector = new GestureDetector(getContext(),this);
        scroller = new Scroller(getContext());
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        paint.setStrokeCap(Paint.Cap.ROUND);
    }

    private PointF center=new PointF(100,100);
    private float widthView = 100;
    private float heightView = 100;

    private RectF rectDraw = new RectF(center.x-50,center.y-50,center.x+50,center.y+50);

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float vWidth = widthView  ;
        float vHeight = heightView ;
        float left = center.x-vWidth;
        float right = center.x+vWidth;
        float top = center.y-vHeight;
        float bottom = center.y+vHeight;
        rectDraw.set(left,top,right,bottom);
        Log.d("绘制位置","==>"+rectDraw.toString());
        paint.setColor(Color.RED);
        canvas.drawRect(rectDraw,paint);

        canvas.translate(300,300);
        paint.setColor(Color.BLUE);
        canvas.drawRect(rectDraw,paint);

        canvas.translate(-300,-300);
        paint.setColor(Color.GRAY);
        canvas.drawRect(rectDraw,paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
            if(gestureDetector.onTouchEvent(event)){
                return true;
            }
        return super.onTouchEvent(event);
    }

    private float currentScale = 1f;

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        if (mIsFlinging) {
            if (scroller!=null)	//fix for edit mode in the IDE
                scroller.abortAnimation();
            mIsFlinging = false;
        }
        scrollTo( (int)motionEvent.getX() ,(int)motionEvent.getY());
        return true;
    }

    @Override
    public void scrollTo(int x, int y) {
        Log.d("目标点","位置x==>"+x+"-->"+y);
        center.set(x,y);
//        super.scrollTo(x, y);
//        ThreadUtils
        postInvalidate();
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {
//        Log.d("手势事件","onShowPress点击："+motionEvent.getX()+"<--->"+motionEvent.getY());
    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
//        Log.d("手势事件","点击："+motionEvent.getX()+"<--->"+motionEvent.getY());
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float distanceX, float distanceY) {
//        Log.d("手势事件","滚动1："+motionEvent.getX()+"<--->"+motionEvent.getY());//表示的是滚动起始位置
//        Log.d("手势事件","滚动2："+motionEvent1.getX()+"<--->"+motionEvent1.getY());//当前滚动到的位置
//        Log.d("手势事件","滚动3："+distanceX+"<--->"+distanceY);
        scrollTo((int) motionEvent1.getX(), (int)  motionEvent1.getY());
//        scrollBy((int) distanceX, (int) distanceY);
        return true;
    }

    @Override
    public void scrollBy(int distanceX, int distanceY) {
        scrollTo(((int)center.x) + distanceX, ((int)center.y)  + distanceY);
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {
//        Log.d("手势事件","长按点击："+motionEvent.getX()+"<--->"+motionEvent.getY());

    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float velocityX, float velocityY) {
        Log.d("手势事件","扔1："+motionEvent.getX()+"<--->"+motionEvent.getY());
        Log.d("手势事件","扔2："+motionEvent1.getX()+"<--->"+motionEvent1.getY());
        Log.d("手势事件","扔3："+velocityX+"<--->"+velocityY);
        mIsFlinging = true;
//        Point v = new Point((int)velocityX, (int)velocityY);
        scroller.fling((int) motionEvent.getX(), (int) motionEvent.getY(), ((int)velocityX), ((int)velocityY),
                Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
        return true;
    }

    private static final String TAG = "手势事件";
    private boolean mIsFlinging = false;

    @Override
    public void computeScroll() {
        super.computeScroll();
        Log.d(TAG,"滑动效果:"+scroller.getCurrX()+"---"+scroller.getCurrY());
        Log.d(TAG,"滑动效果:"+scroller.getStartX()+"---"+scroller.getStartY());
        Log.d(TAG,"滑动效果:"+scroller.getFinalX()+"---"+scroller.getFinalY());
//        Log.d(TAG,"必须要使用这个才能连续滑动:"+(!scroller.isFinished()));
//        Log.d(TAG,"必须要使用这个才能连续时间滑动:"+(scroller.getDuration()));
        if (scroller == null) { //fix for edit mode in the IDE
            return;
        }
        if (!mIsFlinging) {
            return;
        }
        if (!scroller.computeScrollOffset()) {
            return;
        }
        //使用这个判断才能达到惯性滑动的效果
        if (scroller.isFinished()) {
            // we deliberately ignore the very last scrollTo, which sometimes provokes map hiccups
            mIsFlinging = false;
        } else {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());
            //必须要使用这个才能连续滑动
//            postInvalidate();
        }
    }
}
